/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.ucagent.database.dao;

import cn.net.greenet.db.HibernateUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.net.greenet.db.bean.Dpiendpointbean;
import cn.net.greenet.db.bean.Policyrepositoryinstance;
import cn.net.greenet.db.bean.Versionmanaged;
import cn.net.greenet.db.bean.Versionreported;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author hedingwei
 */
public class DPIEndPointDao {

    public List<Dpiendpointbean> getAllDPIEndPoints() {
        List<Dpiendpointbean> beans = new ArrayList<Dpiendpointbean>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            List list = session.createQuery("FROM Dpiendpointbean").list();
            for (Object o : list) {
                beans.add((Dpiendpointbean) o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    /**
     * 获取一个DPI对象
     *
     * @param name
     * @return
     */
    public Dpiendpointbean getDPIEndPointBeanByName(String name) {
        Session session = null;
        Dpiendpointbean bean = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT dpi FROM Dpiendpointbean dpi where dpi.name=:name");
            query.setParameter("name", name);
            bean = (Dpiendpointbean) query.uniqueResult();
        } catch (Exception e) {
        } finally {
            session.close();
        }
        return bean;
    }

    /**
     * 获取一个DPI所上报的所有的<策略类型，策略版本>对应关系
     *
     * @param name
     * @return
     */
    public Map<String, Long> getDPIVersionreported(String name) {
        Map<String, Long> map = new HashMap<String, Long>();
        Session session = null;
        Dpiendpointbean bean = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT vr FROM Versionreported vr where vr.dpiendpointbean.name=:name");
            query.setParameter("name", name);
            List list = query.list();
            for (Object o : list) {
                Versionreported vr = (Versionreported) o;
                map.put(vr.getPolicyrepositoryinstance().getPolicyrepositorytype().getMessageType(), vr.getMessageSerialNo());
            }
        } catch (Exception e) {
        } finally {
            session.close();
        }
        return map;
    }

    /**
     * 获取一个DPI托管后的所有的<策略类型，策略版本>对应关系
     *
     * @param name
     * @return
     */
    public Map<String, Long> getDPIVersionmanaged(String name) {
        Map<String, Long> map = new HashMap<String, Long>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT vr FROM Versionmanaged vr where vr.dpiendpointbean.name=:name");
            query.setParameter("name", name);
            List list = query.list();
            for (Object o : list) {
                Versionmanaged vr = (Versionmanaged) o;
                map.put(vr.getPolicyrepositoryinstance().getPolicyrepositorytype().getMessageType(), vr.getMessageSerialNo());
            }
        } catch (Exception e) {
        } finally {
            session.close();
        }
        return map;
    }

    public Policyrepositoryinstance getPolicyrepositoryinstance(String name, String messageType) {
        Session session = null;
        Policyrepositoryinstance bean = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT pri FROM Policyrepositoryinstance pri join pri.dpiendpointbeans dpi where dpi.name=:name and pri.policyRepositoryType.messageType=:messageType");
            query.setParameter("name", name);
            query.setParameter("messageType", messageType);
            bean = (Policyrepositoryinstance) query.uniqueResult();
            return bean;
        } catch (Exception e) {
            return null;
        } finally {
            session.close();
        }
    }

    public Policyrepositoryinstance getPolicyrepositoryinstance(int dpiId, String messageType) {
        Session session = null;
        Policyrepositoryinstance bean = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT pri FROM Policyrepositoryinstance pri join pri.dpiendpointbeans dpi where dpi.id=:id and pri.policyRepositoryType.messageType=:messageType");
            query.setParameter("id", dpiId);
            query.setParameter("messageType", messageType);
            bean = (Policyrepositoryinstance) query.uniqueResult();
            return bean;
        } catch (Exception e) {
            return null;
        } finally {
            session.close();
        }
    }
    
    /**
     * 获取一个DPI所配置的所有策略仓库。返回以策略仓库类型，即MessageType做key形成一个map
     *
     * @param name
     * @return
     *
     */
    public Map<String, Policyrepositoryinstance> getDPIConfiguredPRTypeInstance(String name) {
        Map<String, Policyrepositoryinstance> map = new HashMap<String, Policyrepositoryinstance>();
        Session session = null;
        Dpiendpointbean bean = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT dpi FROM Dpiendpointbean dpi where dpi.name=:name");
            query.setParameter("name", name);
            bean = (Dpiendpointbean) query.uniqueResult();
            for (Object o : bean.getPolicyrepositoryinstances()) {
                Policyrepositoryinstance pri = (Policyrepositoryinstance) o;
                map.put(pri.getPolicyrepositorytype().getMessageType(), pri);
            }
        } catch (Exception e) {
        } finally {
            session.close();
        }
        return map;
    }

    public static void main(String[] args) {
        DPIEndPointDao dao = new DPIEndPointDao();
        System.out.println(dao.getAllDPIEndPoints());
        System.out.println("DDDDDDDDDDDDDd");
        System.out.println(dao.getDPIEndPointBeanByName("gn-1"));
        System.out.println(dao.getDPIConfiguredPRTypeInstance("gn-1"));
        System.out.println(dao.getDPIVersionreported("gn-1"));
        System.out.println(dao.getDPIVersionmanaged("gn-1"));
        HibernateUtil.getSessionFactory().close();
    }

}
